package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import po.Client;
import service.ClientService;
import service.impl.ClientServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
    	response.setHeader("content-type","text/html;charset=UTF-8");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		Client c = new Client(name, password);
		HttpSession session = request.getSession();
		ClientService clientService = new ClientServiceImpl();
		int cId = clientService.CheckClient(c);
		System.out.println("cId=="+cId);
		if( cId > 0){
			request.setAttribute("success","登录成功");
			session.setAttribute("name",name);
			session.setAttribute("password", password);
			session.setAttribute("cId", cId);
			request.getRequestDispatcher("ShowDishServlet").forward(request, response);
		}else{
			request.setAttribute("error", "登录失败！");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}

}
